//
//  LLPriceView.m
//  lrlzapp
//
//  Created by dl on 16/2/23.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLPriceView.h"

@interface LLPriceView ()
@property(nonatomic, assign) CGFloat money;
@property(nonatomic, assign) CGFloat gold;

// 此属性跟上面俩是或者关系
@property(nonatomic, copy) NSString *moneyAndGold;
@end

@implementation LLPriceView {
    TTTAttributedLabel *priceLabel;
    UIImageView *goldImg;
    TTTAttributedLabel *goldLabel;
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self reset];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self reset];
    }
    return self;
}

- (void)reset {
    priceLabel = [TTTAttributedLabel new];
    [self addSubview:priceLabel];

    goldImg = [UIImageView new];
    [goldImg setImage:[UIImage imageNamed:@"cart_goods_gold"]];
    goldImg.hidden = YES;
    [self addSubview:goldImg];

    goldLabel = [TTTAttributedLabel new];
    [self addSubview:goldLabel];
}

- (void)layoutSubviews {
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.height.equalTo(self);
    }];
    [goldImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(priceLabel.mas_right).offset(SUIT_W(3));
        make.centerY.equalTo(priceLabel);
        make.width.height.mas_equalTo(SUIT_H(14));
    }];
    [goldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(goldImg.mas_right).offset(SUIT_W(3));
        make.bottom.height.equalTo(priceLabel);
        make.right.equalTo(self);
    }];
}

- (void)setMoney:(CGFloat)money gold:(CGFloat)gold {
    if (_money != money || _gold != gold) {
        _money = money;
        _gold = gold;

        if (_money > 0) {
            NSString *moneyStr = [NSString stringWithFormat:@"%.2lf%@", _money, _gold > 0 ? @" + " : @""];

            NSMutableAttributedString *priceAttr = [[NSMutableAttributedString alloc] initWithString:moneyStr];
//            [priceAttr addAttribute:NSFontAttributeName value:SUIT_FONT_320(20) range:NSMakeRange(0, moneyStr.length-3)];
//            [priceAttr addAttribute:NSFontAttributeName value:SUIT_FONT_320(14) range:NSMakeRange(moneyStr.length-3, 3)];
            [priceAttr addAttribute:NSFontAttributeName value:SUIT_FONT(14) range:NSMakeRange(0, moneyStr.length)];
            [priceAttr addAttribute:NSForegroundColorAttributeName value:(id) UI_COLOR_RED.CGColor range:NSMakeRange(0, moneyStr.length)];
            NSRange plusRange = [moneyStr rangeOfString:@"+"];
            if (plusRange.location != NSNotFound) {
                [priceAttr addAttribute:NSForegroundColorAttributeName value:(id) UI_COLOR_FONT_BLACK.CGColor range:plusRange];
            }
            priceLabel.attributedText = priceAttr;
            CGSize size = [TTTAttributedLabel sizeThatFitsAttributedString:priceAttr withConstraints:CGSizeMake(100, 0) limitedToNumberOfLines:0];
            [priceLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(size.width);
            }];
        } else {
            [priceLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(0);
            }];
        }
        if (_gold > 0) {
            goldImg.hidden = NO;
            NSString *goldStr = [NSString stringWithFormat:@"%.lf", _gold];
            NSMutableAttributedString *goldAttr = [[NSMutableAttributedString alloc] initWithString:goldStr];
            [goldAttr addAttribute:NSFontAttributeName value:SUIT_FONT(14) range:NSMakeRange(0, goldStr.length)];
            [goldAttr addAttribute:NSForegroundColorAttributeName value:(id) UI_COLOR_FONT_BLACK.CGColor range:NSMakeRange(0, goldStr.length)];
            goldLabel.attributedText = goldAttr;
        }
    }
}

- (void)setGoods:(LLCartGoods *)goods {
    if (goods.buyMode == GoodsBuyModeMoney) {
        [self setMoney:goods.price gold:0];
    } else if (goods.buyMode == GoodsBuyModeGold) {
        [self setMoney:0 gold:goods.point];
    } else if (goods.buyMode == GoodsBuyModeMoneyAndGold) {
        [self setMoney:goods.money gold:goods.gold];
    }
}

- (void)setMoneyAndGold:(NSString *)moneyAndGold {
    if (_moneyAndGold != moneyAndGold) {
        _moneyAndGold = [moneyAndGold copy];

        CGFloat money = 0, golg = 0;
        NSArray *arr = [_moneyAndGold componentsSeparatedByString:@"+"];
        if (arr.count > 0) {
            money = [arr[0] floatValue];
        }
        if (arr.count > 1) {
            golg = [arr[1] floatValue];
        }
        [self setMoney:money gold:golg];
    }
}

- (CGFloat)getMoney {
    return _money;
}

- (CGFloat)getGold {
    return _gold;
}

@end